package com.yanqu.road.server.manager.activity.commonsupport;

import com.yanqu.road.entity.activity.ActivityConfig;
import com.yanqu.road.entity.activity.ActivityInfo;
import com.yanqu.road.entity.activity.commonsupport.config.CommonUnionSupportConfig;
import com.yanqu.road.entity.enums.eActivityType;
import com.yanqu.road.entity.union.activitygroup.UnionActivityGroup;
import com.yanqu.road.logic.bussiness.servercenter.ActivityBussiness;
import com.yanqu.road.logic.helper.ActivityHelper;
import com.yanqu.road.logic.helper.UnionActivityGroupHelper;
import com.yanqu.road.server.CrossBaseServer;
import com.yanqu.road.server.TempCrossMgr;
import com.yanqu.road.server.manager.activity.ActivityMgr;
import com.yanqu.road.server.manager.activity.commonsupport.handler.*;
import com.yanqu.road.server.manager.union.activitygroup.Cross2UnionActivityGroupMgr;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import java.util.*;
import java.util.concurrent.ConcurrentHashMap;

public class CommonSupportMgr extends TempCrossMgr {

    private static Map<Integer, Map<Integer, CommonSupportActivity>> activityMap = new ConcurrentHashMap<>();

    private static Logger logger = LogManager.getLogger(CommonSupportMgr.class.getName());

    @Override
    public boolean removeExpireData() throws Exception {
        return false;
    }

    @Override
    public boolean init() throws Exception {
        SupportActivityHandler.put(eActivityType.CrossUnionWarActivity.getValue(), new QunYingSupportActivityHandler());
        SupportActivityHandler.put(eActivityType.JiuZhou.getValue(), new JiuZhouSupportActivityHandler());
        SupportActivityHandler.put(eActivityType.SeacraftActivity.getValue(), new SeacraftActivitySupportHandler());
        SupportActivityHandler.put(eActivityType.StrongholdWarActivity.getValue(), new StrongholdWarActivitySupportHandler());
        SupportActivityHandler.put(eActivityType.PeakStrongHold.getValue(), new PeakStrongHoldSupportHandler());
        SupportActivityHandler.put(eActivityType.PeakSeacraftActivity.getValue(), new PeakSeacraftActivitySupportHandler());
        SupportActivityHandler.put(eActivityType.RankUnionWar.getValue(), new RankUnionWarSupportHandler());
        SupportActivityHandler.put(eActivityType.SuiTang.getValue(), new SuiTangSupportActivityHandler());
        return reload();
    }

    @Override
    public boolean reloadConfig() throws Exception {
        return true;
    }

    @Override
    public boolean reloadData() throws Exception {
        Set<Integer> typeList = new HashSet<>();
        Map<Integer, ActivityInfo> activityInfoMap = ActivityMgr.getOpenCrossActivityMapByTypes(typeList);
        //活动加载进内存
        for (ActivityInfo activityInfo : activityInfoMap.values()) {
            //活动是否在展示期
            if (!ActivityHelper.activityInShowTime(activityInfo)) {
                continue;
            }
            //判断活动是否需要分组
            if (UnionActivityGroupHelper.needGroup(activityInfo)) {
                //查询活动分组
                Map<Integer, UnionActivityGroup> activityGroupMap = Cross2UnionActivityGroupMgr.getGroupMap(activityInfo.getActivityId());
                if (activityGroupMap == null || activityGroupMap.isEmpty()) {
                    continue;
                }
                for (UnionActivityGroup activityGroup : activityGroupMap.values()) {
                    if (activityGroup.getCrossServerId() == CrossBaseServer.getInstance().getServerId()) {
                        getActivity(activityGroup.getActivityId(), activityGroup.getGroupId());
                    }
                }
            }else {
                getActivity(activityInfo.getActivityId(), 0);
            }
        }
        return true;
    }

    /**
     * 初始化活动
     * @param activityId
     * @return
     */
    public static CommonSupportActivity getActivity(int activityId, int groupId) {
        if(groupId < 0){
            getLogger().error("活动 {}, 分组入参 {} 错误", activityId, groupId);
            return null;
        }
        Map<Integer, CommonSupportActivity> map = activityMap.get(activityId);
        if(map == null){
            synchronized (activityMap){
                map = activityMap.get(activityId);
                if(map == null){
                    map = new ConcurrentHashMap<>();
                    activityMap.put(activityId, map);
                }
            }
        }
        CommonSupportActivity supportActivity = map.get(groupId);
        if(supportActivity == null){
            synchronized (map){
                supportActivity = map.get(groupId);
                if(supportActivity == null){
                    // 读配置 获取活动信息 保证有活动信息
                    ActivityInfo activityInfo = ActivityMgr.getActivityInfoMayLoadDB(activityId);
                    if (activityInfo == null) {
                        return null;
                    }
                    //不在展示期也不处理了
                    if (!ActivityHelper.activityInShowTime(activityInfo)) {
                        return null;
                    }
                    //构造活动配置
                    CommonUnionSupportConfig config = getConfigFromDb(activityInfo);
                    //创建活动
                    supportActivity = new CommonSupportActivity(config, groupId);
                    map.put(groupId, supportActivity);
                }
            }
        }
        return supportActivity;
    }

    public static CommonUnionSupportConfig getConfigFromDb(ActivityInfo activityInfo) {
        List<Integer> activityIdList = new ArrayList<>();
        activityIdList.add(activityInfo.getActivityId());
        Map<String, ActivityConfig> activityConfigMap = ActivityBussiness.getActivityConfigMap(activityIdList).get(activityInfo.getActivityId());
        return new CommonUnionSupportConfig(activityInfo, activityConfigMap);
    }

    @Override
    public boolean save() {
        for (Map.Entry<Integer, Map<Integer, CommonSupportActivity>> entry : activityMap.entrySet()) {
            for (Map.Entry<Integer, CommonSupportActivity> activityEntry : entry.getValue().entrySet()) {
                activityEntry.getValue().saveData();
            }
        }
        return true;
    }

    @Override
    public boolean stop() {
        return false;
    }

    public static void repair(){

    }
}
